package com.te;

import java.util.ArrayList;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.CircleDef;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;

import android.util.Log;

public class PhysicsWorld {

    private AABB           aABB;
    public World           world;
    public ArrayList<Body> bodies     = new ArrayList<Body>();
    private BodyDef        groundBodyDef;
    private PolygonDef     groundShapeDef;
    protected float        timeStep   = 1f / 6f;
    public int             iterations = 8;
    private String         TAG        = PhysicsWorld.class.getSimpleName();
    public boolean         touch      = false;
    float                  x, y;

    public void create() {

        // Step 1: Create Physics World Boundaries
        aABB = new AABB();
        aABB.lowerBound.set(new Vec2((float) 0.0, (float) 0.0));
        aABB.upperBound.set(new Vec2((float) 300.0, (float) 300.0));

        // Step 2: Create Physics World with Gravity
        Vec2 gravity = new Vec2((float) 5.0, (float) -10.0);
        boolean doSleep = true;
        world = new World(aABB, gravity, doSleep);

        // Step 3: Create Ground Box
        BodyUtil.createBorders(world, 4, 0.2f, 0.1f, 0.1f, 5, 9);

        /*
         * groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 0.0, (float) 0.0)); Body groundBody1 =
         * world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float) 300.0, (float)
         * 10.0); groundBody1.createShape(groundShapeDef);
         * 
         * groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 200.0, (float) 0.0)); Body groundBody2 =
         * world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float) 10.0, (float)
         * 350.0); groundBody2.createShape(groundShapeDef);
         * 
         * groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 0.0, (float) 250.0)); Body groundBody3 =
         * world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float) 300.0, (float)
         * 10.0); groundBody3.createShape(groundShapeDef);
         * 
         * groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 0.0, (float) 0.0)); Body groundBody4 =
         * world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float) 10.0, (float)
         * 300.0); groundBody4.createShape(groundShapeDef);
         */
    }

    public boolean addBall(float x, float y) {
        // Create Dynamic Body

        if (touch) {

            BodyDef bodyDef = new BodyDef();
            Log.d(TAG, "adding ball x,y " + x + " : " + y);
            bodyDef.position.set(x, y);
            Body body = world.createBody(bodyDef);

            // Create Shape with Properties
            CircleDef circle = new CircleDef();
            circle.radius = (float) 1.8;
            circle.density = (float) 0.5;

            if (body != null) {
                // Assign shape to Body
                body.createShape(circle);
                body.setMassFromShapes();

                // Increase Counter
                bodies.add(body);
                return true;
            }
            else {
                Log.e(TAG, "Failed to add body");
            }

            return false;
        }
        else {
            touch = true;
            this.x = x;
            this.y = y;
            return true;
        }
    }

    public void update() {
        // Update Physics World
        // Log.d("phy", "timeStep " + timeStep + " it " + iterations);
        world.step(timeStep, iterations);
        if (touch) {
            addBall(x, y);
            touch = false;
        }
        // Log.d(TAG, "world bodies " + world.getBodyCount() + " gravity: " + world.getGravity().x + "," + world.getGravity().y);
        /*
         * Body body = world.getBodyList(); while (body != null) { Log.d(TAG, "body "); Log.d(TAG, " x " + body.getPosition().x +
         * " y " + body.getPosition().y); body = body.getNext(); }
         */

        /*
         * for (Body body: world.getBodyList().) {
         * 
         * for(int k = 0; k < world.getBodyCount(); k++) { world.getBodyList() Log.d(TAG,"Vec2 position = bodies[k].getPosition();
         * float angle = bodies[k].getAngle(); Log.v("Physics Test", "Pos: (" + position.x + ", " + position.y + "), Angle: " +
         * angle); } }
         */

    }
}
